package com.google.code.swingonfire.showcase.graphic.sketch;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;

import com.google.code.eventsonfire.Events;
import com.google.code.eventsonfire.awt.AWTEvents;
import com.google.code.eventsonfire.swing.SwingEventHandler;
import com.google.code.eventsonfire.swing.SwingEvents;
import com.google.code.swingonfire.border.ShadowBorder;
import com.google.code.swingonfire.builder.JLabelBuilder;
import com.google.code.swingonfire.builder.JListBuilder;
import com.google.code.swingonfire.builder.JScrollPaneBuilder;
import com.google.code.swingonfire.component.JBorderPanel;
import com.google.code.swingonfire.component.foldable.FoldableEvent;
import com.google.code.swingonfire.component.foldable.FoldableListener;
import com.google.code.swingonfire.component.foldable.JFoldablePanel;
import com.google.code.swingonfire.component.foldable.JFoldablePanelBuilder;
import com.google.code.swingonfire.component.page.old.JPageTransitionAnimationFactory;
import com.google.code.swingonfire.component.page.old.JPageViewport;
import com.google.code.swingonfire.component.page.old.JPageViewportBuilder;
import com.google.code.swingonfire.showcase.TitlePanel;

public class SketchShowcase extends JPanel implements FoldableListener
{

    private static final long serialVersionUID = 2226843281136750968L;

    private final JPageViewport plotterViewport;
    private final JFoldablePanel sampleListFolder;
    private final JPageViewport parameterViewport;
    private final JFoldablePanel parameterFolder;

    private final JList sketchList;

    private PlotterComponent plotterComponent;
    private SketchParameterPanel sketchParameterPanel;

    public SketchShowcase()
    {
        super(new BorderLayout());

        plotterViewport =
            new JPageViewportBuilder().animationFactory(JPageTransitionAnimationFactory.fadeInAndShrink(0.5))
                .opaque(true).background(Color.WHITE).build();

        JPanel sketchPanel = new JPanel(new BorderLayout());

        sketchList =
            Events.bind(
                new JListBuilder(SketchSample.values()).listener(SwingEvents.fireOnListSelection())
                    .selected(SketchSample.ARROW).cellRenderer(new SketchListCellRenderer()).background(Color.WHITE)
                    .build(), this);

        sampleListFolder =
            Events.bind(
                new JFoldablePanelBuilder(sketchList, new JLabelBuilder("Sketches").fontScale(1.2).fontStyle(Font.BOLD)
                    .build()).listener("sampleListFolding", AWTEvents.fireOnAction(), this).opaque(true)
                    .background(Color.WHITE).build(), this);

        parameterViewport =
            new JPageViewportBuilder().animationFactory(JPageTransitionAnimationFactory.fadeInAndShrink(0.5))
                .preferredSize(320, 200).build();

        parameterFolder =
            Events.bind(
                new JFoldablePanelBuilder(parameterViewport, new JLabelBuilder("Sketch Parameters").fontScale(1.2)
                    .fontStyle(Font.BOLD).build()).listener("parameterFolding", AWTEvents.fireOnAction(), this)
                    .opaque(true).background(Color.WHITE).build(), this);

        sketchPanel.add(new JBorderPanel(sampleListFolder, ShadowBorder.create()), BorderLayout.WEST);
        sketchPanel.add(new JBorderPanel(parameterFolder, ShadowBorder.create()), BorderLayout.CENTER);

        add(new JBorderPanel(new TitlePanel("Plotter and Sketches", "Simple vector graphics"), ShadowBorder.create()),
            BorderLayout.NORTH);

        add(new JBorderPanel(plotterViewport, ShadowBorder.create()), BorderLayout.CENTER);
        add(sketchPanel, BorderLayout.SOUTH);

        setSample((SketchSample) sketchList.getSelectedValue());
    }

    private void setSample(SketchSample selectedValue)
    {
        plotterComponent = new PlotterComponent();
        sketchParameterPanel = new SketchParameterPanel(selectedValue, plotterComponent);
        sketchParameterPanel.updatePlotterComponent();

        plotterViewport.setComponent(plotterComponent);
        parameterViewport.setComponent(new JScrollPaneBuilder(sketchParameterPanel).border(
            BorderFactory.createEmptyBorder()).build());
    }

    @SwingEventHandler(event = ActionEvent.class, eachTag = "sampleListFolding")
    public void onSampleListFolding()
    {
        parameterFolder.setFolded(sampleListFolder.isFolded());
    }

    @SwingEventHandler(event = ActionEvent.class, eachTag = "parameterFolding")
    public void onParameterFolding()
    {
        sampleListFolder.setFolded(parameterFolder.isFolded());
    }

    @SwingEventHandler(event = ListSelectionEvent.class)
    public void onListSelection()
    {
        SketchSample selectedValue = (SketchSample) sketchList.getSelectedValue();

        setSample(selectedValue);
    }

    @Override
    public void foldingInProgress(FoldableEvent event)
    {
        validate();
        repaint();
    }

}
